#include "gd32f30x.h"
#include "string.h"
#include "stdint.h"
#include "stdio.h"
#include "version.h"

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

#include "DevicesQueue.h"
#include "DevicesKey.h"
#include "DevicesLed.h"

#include "taskKey.h"


TaskHandle_t g_TaskKeyHand = NULL;   /* 按键消息处理任务句柄 */


void vTaskKey(void *pvParameters)
{
    KeyTypeDef typeKeyData = {0};

    while (1)
    {
        /* 等待任务消息 */
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);

        /* 获取按键消息 */
        while(enumQueuePopDatas(&g_TypeQueueKeyInput, (uint8_t *)&typeKeyData, sizeof(typeKeyData)) == queueNormal)
        {
            /* 短按、并已经松开按键 */
            if(typeKeyData.state == (keyShort | keyCut))
            {
                if(typeKeyData.valueLoosen == KEY_POWER)
                {
                    enumQueuePushDatas(&g_TypeQueueNetworkUdp1Send, "(typeKeyData.valueLoosen == KEY_POWER).\r\n", strlen("(typeKeyData.valueLoosen == KEY_POWER).\r\n"));
                }
                else if(typeKeyData.valueLoosen == KEY_RESET)
                {
                    enumQueuePushDatas(&g_TypeQueueNetworkTcp1Send, "(typeKeyData.valueLoosen == KEY_RESET).\r\n", strlen("(typeKeyData.valueLoosen == KEY_RESET).\r\n"));
                }
            }
            /* 长按、并还没有松开按键 */
            else if(typeKeyData.state == (keyLong | keyAdd))
            {
                if(typeKeyData.valuePress == KEY_POWER)
                {
                    enumQueuePushDatas(&g_TypeQueueNetworkUdp1Send, "(typeKeyData.valuePress == KEY_POWER).\r\n", strlen("(typeKeyData.valuePress == KEY_POWER).\r\n"));
                }
                else if(typeKeyData.valuePress == KEY_RESET)
                {
                    enumQueuePushDatas(&g_TypeQueueNetworkTcp1Send, "(typeKeyData.valuePress == KEY_RESET).\r\n", strlen("(typeKeyData.valuePress == KEY_RESET).\r\n"));
                }
            }
        }
    }
}
